23 #define foreach(x, v) for (typeof (v).begin() x=(v).begin(); x !=(v).end(); ++x)
24 #define For(i, a, b) for (int i=(a); i<(b); ++i)
25 #define D(x) cout << #x " is " << x << endl
27 const int mod
= 1000000000;
31 vector
< vector
<int> > data
;
32 Matrix(int k
) :size(k
), data(k
, vector
<int>(k
, 0)) { }
35 Matrix
operator * (const Matrix
&a
, const Matrix
&b
) {
37 for (int i
= 0; i
< a
.size
; ++i
) {
38 for (int j
= 0; j
< a
.size
; ++j
) {
39 for (int k
= 0; k
< a
.size
; ++k
) {
40 ans
.data
[i
][j
] += (1LL * a
.data
[i
][k
] * b
.data
[k
][j
]) % mod
;
41 ans
.data
[i
][j
] %= mod
;
48 Matrix
pow(const Matrix
&m
, int n
) {
50 if (n
% 2 == 1) return m
* pow(m
, n
- 1);
51 Matrix half
= pow(m
, n
/ 2);
60 for (int i
= 0; i
< k
; ++i
) cin
>> b
[i
];
62 for (int i
= 0; i
< k
- 1; ++i
) {
65 for (int j
= k
- 1; j
>= 0; --j
) {
66 cin
>> m
.data
[k
- 1][j
];
69 if (n
> 1) m
= pow(m
, n
- 1);
72 for (int i
= 0; i
< k
; ++i
) {
73 ans
+= (1LL * m
.data
[0][i
] * b
[i
]) % mod
;